<template>
  <div>
    <div class="home">
      <!-- 首页 -->
      <div class="container">
        <!-- 头部 -->
        <div class="head">
          <div class="add" @click="jump('edit')">
            <span class="iconfont">
              &#xe622;
            </span>
          </div>
          <div class="search" @click="jump('search')">
            
              羊蝎子火锅
            
            <div class="iconfont">
              &#xeb86;
            </div>
          </div>
          <div class="icon">
            <div class="iconfont" @click="jump('branch')">&#xe60b;</div>
            <div class="iconfont">&#xe60f;</div>
          </div>
        </div>
        <!-- 广告轮播 -->
        <div class="banner">
        <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
          <van-swipe-item v-for="(item,index) in banner" :key="index">
            <img :src="item.i" alt="">
          </van-swipe-item>
          
        </van-swipe>
        </div>
        <div class="main">
          <div class="list" v-for="(item,index) in list" :key="index">
            <div class="list_top">
              <div class="top_le" @click="user(item.r)">
                <div class="img">
                  <img :src="item.r.a.p" alt="">
                </div>
                <h3>{{item.r.a.n}}</h3>
                <em>LV.{{ item.r.a.lvl}}</em>
              </div>
              <div class="top_rig iconfont">
              &#xeb10;
              </div>
            </div>
            <div class="list_cont">
              <div class="img" @click="go(item.r.id)">
                <img :src="item.r.img" alt="">
              </div>
              <div class="info">
                <div class="in_le">
                  <div class="img">
                    <img :src="item.p" alt="" :style="{'z-index':index,'left':index*15+'px'}" 
                    v-for="(item,index) in item.r.collect_users" :key="index">
                  </div>
                  <span>{{item.r.ph}}人收藏</span>
                </div>
                <div class="collect" @click="collect(item.r.id,$event)">
                  <i class=""></i>
                </div>
              </div>
            </div>
            <div class="list_bottom">
              <h2>{{ item.r.stdname}}</h2>
              <div class="label" v-if="item.r.trim_title">
                <em class="iconfont">
                  &#xe719;
                </em>
                花式炖煮不停歇
              </div>
            </div>
          </div>
         
        </div>
      </div>

    </div>
  </div>
</template>

<script>
import { ref } from "vue"
import { reactive, onUpdated } from '@vue/runtime-core'
import { getJsonHome } from "../api/home.js";
import { useRouter } from 'vue-router'
import { useStore } from "../store/index";
export default {
 setup(){
    let store = useStore(); 
    let router = useRouter();
    let obj = reactive({
      name:"jack"
    })
    let banner = ref(null);
    let list = ref(null);
    function go(id) {
      console.log(id);
      router.push({
        path: "/about",
        query: {
          id
        }
      })
    }
    function user(user) {//进入用户页
      let id = user.a.id
      router.push({
        path: "/person",
        query: {
          id
        }
      })
    }
    function collect(id,e) {//添加收藏
      if (e.target.classList.contains("con")){
        let index = store.caihistory.findIndex(item=>{
          return item == id
        })
        console.log(index);
        store.caihistory.splice(index,1)

      } else {
        store.caihistory.push(id);

      }
      e.target.classList.toggle("con");
      console.log(store.caihistory);
    }
    
    function jump(src) {//页面跳转
      router.push({
        path: '/'+src
      })
    }
    getJsonHome().then(data => {
      banner.value = data.result.banner;
      console.log(data);
      list.value = data.result.list.filter(item =>{
        return item.r
      })
    })
    
    function creat() {
      let is = document.querySelectorAll(".collect i");
      list.value.forEach((item,index) =>{
        store.caihistory.forEach(val =>{
          if(val == item.r.id){
            is[index].classList.add("con")
          }
        })
      })
    }
    onUpdated(() => {
      creat()
    })
  return{
    obj,
    banner,
    list,
    go,
    collect,
    user,
    jump

  }
 }
};
</script>

<style lang="scss" scoped>
.home{
  .banner{
    .my-swipe .van-swipe-item {
        color: #fff;
        font-size: 20px;
        line-height: 150px;
        text-align: center;
        background-color: #39a9ed;
        img{
          width: 100%;
          display: block;
        }
      }
  }
  .head{
    padding: 10px 16px;
    display: flex;
    .add{
      padding: 4px 0;
      &>span{
        width: 30px;
        height: 30px;
        line-height: 30px;
        border-radius: 50%;
        font-weight: bold;
        background-color: #FEC801;
        text-align: center;
        display: block;
        font-size: 20px;
      }
    }
    .search{
      &>.iconfont{
        position: absolute;
        left: 5px;
        top: 0;
        font-weight: bold;
      }
      position: relative;
      width: 190px;
      height: 38px;
      padding-left: 25px;
      color: #999999;
      font-size: 14px;
      line-height: 38px;
      background-color: #F7F7F7;
      margin-left: 12px;
    }
    .icon{
      display: flex;
      width: 64px;
      justify-content: space-between;
      margin-left: 20px;
      &>div{
        font-size: 20px;
        line-height: 38px;
      }
    }
  }
  .main{
    .list{
      padding-bottom: 24px;
      .list_top{
        display: flex;
        justify-content: space-between;
        .top_le{
          align-items: center;
          display: flex;
          .img{
            padding: 10px;
            img{
              width: 34px;
              height: 34px;
              border-radius: 50%;
              display: block;
            }
          }
          h3{
            font-size: 13px;
            font-weight: normal;
            margin-right: 10px;
          }
          em{
            color: #E4E385;
            font-size: 14px;
            font-style: italic;
          }
        }
        .top_rig{
          font-size: 18px;
          color: #383838;
          padding-right: 16px;
          line-height: 64px;
        }
      }
      .list_cont{
        min-height: 282px;
        &>.img{
          width: 100%;
          img{
            width: 100%;
          }
        }
        .info{
          display: flex;
          justify-content: space-between;
          padding: 15px 12px;
          .in_le{
            width: 50%;
            display: flex;
            &>.img {
              width: 64px;
              position: relative;
              img {
                width: 20px;
                height: 20px;
                border-radius: 50%;
                position: absolute;
              }
            }
            span{
              padding-left: 10px;
              font-size: 13px;
            }
          }
          .collect{
            &>i{
              width: 20px;
              height: 20px;
              display: block;
              background: url('../assets/img_gu/c1.png') no-repeat;
              background-size: 100%;
            }
            &>.con{
              background: url('../assets/img_gu/c0.png') no-repeat;
              background-size: 100%;
            }
          }
        }
      }
      .list_bottom{
        padding-left: 12px;
        h2{
          font-size: 15px;
          font-weight: normal;
          padding-bottom: 15px;
        }
        .label{
          font-size: 13px;
          color: #108A90;
        }
      }
    }
  }

}
</style>